﻿using System;
using System.Collections; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cwtrock.dao.impl;
using cwtrock.model;

namespace cwtrock.service.impl
{
    class VoucherServiceImpl
    {
        private VoucherDaoImpl voucherDaoImpl = new VoucherDaoImpl();
        private VrecordsDaoImpl vrecordDaoImpl = new VrecordsDaoImpl();


        public Vouchers save(Vouchers voucher)
        {

          return  voucherDaoImpl.save(voucher);
        }

        // 按用户取凭证集合
        public List<Vouchers> getVouchersByUserId(string userid) {

            return voucherDaoImpl.getVouchersByUserId(userid);
        }

        // 判断该用户当前顺序号可存在
        public int getVouchersCntByUserAndSeqno(string seqno, string userid) {

            return voucherDaoImpl.getVouchersCntByUserAndSeqno(seqno, userid);
        }

        // 判断该用户当前类别的凭证号可存在
        public int getVouchersCntByUserAndVcode(string vcode, string vtype, string userid)
        {

            return voucherDaoImpl.getVouchersCntByUserAndVcode(vcode, vtype,userid);
        }

        // 按条件查询凭证
        public List<Vouchers> getVouchersByCondition(Vouchers vou)
        {
            return voucherDaoImpl.getVouchersByCondition(vou);
        }

        // 按凭证id取得凭证
        public Vouchers getVouchersByVouid(List<Vouchers> vouls, long vid)
        {
            if (vouls == null)
                return null;

            foreach (Vouchers vou in vouls)
            {
                if (vou.Id.Equals(vid))
                {
                    return vou;
                    
                }

            }


            return null;
        }

        public int update(Vouchers voucher) {

            return voucherDaoImpl.update(voucher);
        }

        public int delete(Vouchers voucher) {

            return voucherDaoImpl.delete(voucher);
        }

        // 凭证过账
        public int postVoucher(Vouchers voucher) {

            int voucherPosted = voucherDaoImpl.postVoucher(voucher);
            return voucherPosted;
        }

        // 数要过账的凭证数
        public int countVoucherTobePosted() {

            return voucherDaoImpl.countVoucherTobePosted();
        }

        // 去总账的凭证数据
        public Dictionary<string, List<Vrecords>> getVrecordsByGeneral(Vouchers voucher_begin, Vouchers voucher_end, Vrecords vrecord_begin, Vrecords vrecord_end)
        {

            Dictionary<string, List<Vrecords>> vrdict = new Dictionary<string, List<Vrecords>>();
            List<Vouchers> vouls = voucherDaoImpl.getVouchersBetween(voucher_begin, voucher_end);

            int size = vouls.Count;
            if (vouls != null && size > 0)
            {
                vrecord_begin.Vid = vouls[0].Id;
                vrecord_end.Vid = vouls[size - 1].Id; 
            
            }
        
            List<Vrecords> recordls = vrecordDaoImpl.getVrecordsBetween(vrecord_begin, vrecord_end);

            if (vouls != null && recordls != null) {

                

                foreach (Vrecords vr in recordls) {

                    List<Vrecords> vrls ;

                    if (!vrdict.ContainsKey(vr.Acctcode))
                    {
                        vrls = new List<Vrecords>();
                        vrls.Add(vr);
                        vrdict.Add(vr.Acctcode, vrls);

                    }
                    else {
                        vrls = (List<Vrecords>)vrdict[vr.Acctcode];
                        vrls.Add(vr);
                        vrdict[vr.Acctcode] = vrls;
                    }
                   
                  



                    long vid = vr.Vid;
                    foreach (Vouchers vou in vouls)
                    {
                        if (vou.Id == vid) {
                            vr.Voucher = vou;
                            break;
                        }
                    }
                
                
                }

            }


            return vrdict;
        
        }
    }
}
